package com.axend.aerosense.base.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.axend.aerosense.base.BaseModuleInit;

/**
 * SharedPreferences工具类，用于内部文件的读取与写入
 * @author ChenHongWei
 * @date :2020/10/9 15:06
 */

public class SharedPreferencesUtils {

    private static SharedPreferencesUtils sharedPreferencesUtils;
    private static String fileName = "aerosense_data";  //默认存储的文件名
    private static Context context;
    private static SharedPreferences shp;

    //单列模式
    public static SharedPreferencesUtils getInstance(){
        if (sharedPreferencesUtils == null){
            sharedPreferencesUtils = new SharedPreferencesUtils();
        }
        context = BaseModuleInit.getAppContext();
        shp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
        return sharedPreferencesUtils;
    }

    public static SharedPreferencesUtils getInstance(String name){
        getInstance();
        shp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        return sharedPreferencesUtils;
    }

    private SharedPreferencesUtils(){}

    /**
     * 传入数据，自动保存为相对应类型的数据
     * @param key
     * @param value
     */
    public void saveData(String key, Object value){
        if (key == null || key.isEmpty()){
            throw new IllegalArgumentException("key不能为空！");
        }else {
            SharedPreferences.Editor editor = shp.edit();
            if (value instanceof String){
                //字符串
                editor.putString(key, String.valueOf(value));
            }else if (value instanceof Integer){
                //整型
                editor.putInt(key, (Integer) value);
            }else if (value instanceof Float){
                //单精度浮点型
                editor.putFloat(key, (Float) value);
            }else if (value instanceof Boolean){
                //布尔型
                editor.putBoolean(key, (Boolean) value);
            }else if (value instanceof Long){
                //长整型
                editor.putLong(key, (Long) value);
            }else {
                throw new IllegalArgumentException("不支持此类型的参数！");
            }

            editor.apply();
        }
    }

    /**
     * 读取数据，读取与defaultValue相同类型的数据
     * @param key
     * @param defaultValue
     * @return
     */
    public Object loadData(String key, Object defaultValue){
        if (key == null || key.isEmpty()){
            throw new IllegalArgumentException("key不能为空！");
        }else {
            if (defaultValue instanceof String){
                //字符串
                return shp.getString(key, String.valueOf(defaultValue));
            }else if (defaultValue instanceof Integer){
                //整型
                return shp.getInt(key, (Integer) defaultValue);
            }else if (defaultValue instanceof Float){
                //单精度浮点型
                return shp.getFloat(key, (Float) defaultValue);
            }else if (defaultValue instanceof Boolean){
                //布尔型
                return shp.getBoolean(key, (Boolean) defaultValue);
            }else if (defaultValue instanceof Long){
                //长整型
                return shp.getLong(key, (Long) defaultValue);
            }else {
                throw new IllegalArgumentException("不支持此类型的参数！");
            }
        }
    }

    /**
     * 保存Token
     */
    public void setToken(String token){
        if (token != null && !token.isEmpty()){
            SharedPreferences.Editor editor = shp.edit();
            editor.putString("token", token);
            //最后一定要记得提交
            editor.apply();
        }
    }

    /**
     * 获取token
     */
    public String getToken(){
        return shp.getString("token", null);
    }

    /**
     * 清除token
     */
    public void clearToken(){
        SharedPreferences.Editor editor = shp.edit();
        editor.putString("token", "");
        //最后一定要记得提交
        editor.apply();
    }

}
